var program_id = 0; /* 节目id */
var page_index = 1; /* 当前页数 */
var pages = 1; /* 总页数 */
var load_leng = 1100; //指定距离加载一次
var old_car_series_id = 0; // 上一次选中的车系

$(document).ready(function() {
	get_user_mess_and_token();
	var url = location.search;
	program_id = url.substr(1);
	if (program_id != 0)
		$(".to_login").attr("href",
			"http://127.0.0.1:8899/html/car_login.html?car_program_all_video/" +
			program_id);
	/* 加载全部数据 */
	load_all_data();
});

/* 加载全部数据 */
function load_all_data() {
	$.post("http://127.0.0.1:8899/api/consumer/PublicController/load_all_program_data/" + program_id, function(data) {
		/* 加载节目信息 */
		var program = data.all_program_data;
		$("#program_name").text(program.program_name);
		$("#program_name").attr("title", program.program_name);
		var days = (new Date().getTime() - new Date(program.lately_release_date).getTime()) / (1000 * 60 * 60 *
			24);
		$("#date_and_count").text(parseInt(days) + "天前更新 · 共" + program.video_total + "期");
		$("#program_brief_introduction").text(program.program_brief_introduction);
		$("#program_background_url").attr("src", "http://120.48.47.144:8899/images/" + program
			.program_background_url);

		var all_car_series_data = "";
		$(data.all_car_series_data).each(function(index, item) {
			all_car_series_data += "<button type=\"button\" onclick=\"selected_car_series(this," + item
				.car_series_id + ")\" class=\"jsx-1697020283 category-item\">" + item.car_series_name +
				"</button>";
		});
		$("#all_car_series").append(all_car_series_data);
		var car_series_width = $("#all_car_series").css("width");
		if (car_series_width.substr(0, car_series_width.indexOf("px")) > 1611) {
			$("#open").show();
		}
		load_all_video(0);
	}, "json");
}

/* 展开/收起 */
function open_put_away(type) {
	if (type == "open") {
		$("#all_car_series").parent().parent().attr("class", "jsx-2758281763 collapse expanded");
		$("#open").attr("class", "jsx-2758281763 btn-wrapper btn-wrapper__expand");
		$("#open button").attr("class", "jsx-2758281763 btn").text("收起");
		$("#open button").attr("onclick", "open_put_away(\"put_away\")");
	} else {
		$("#all_car_series").parent().parent().attr("class", "jsx-2758281763 collapse collapsed");
		$("#open").attr("class", "jsx-2758281763 btn-wrapper btn-wrapper__collapsed");
		$("#open button").attr("class", "jsx-2758281763 btn collapsed").text("展开");
		$("#open button").attr("onclick", "open_put_away(\"open\")");
	}
}

/* 选中车系 */
function selected_car_series(element, car_series_id) { //当前对象，车系id
	$(element).attr("class", "jsx-1697020283 category-item active").siblings().attr("class",
		"jsx-1697020283 category-item");
	$("#all_video div").remove();
	old_car_series_id = car_series_id;
	page_index = 1;
	load_leng = 1100;
	load_all_video(car_series_id);
}

/* 查询全部视频 */
function load_all_video(car_series_id) {
	$.post("http://127.0.0.1:8899/api/consumer/VideoController/select_appoint_program_all_video/" + program_id + "/" +
		car_series_id + "/" + page_index,
		function(data) {
			pages = data.pages;
			var all_video_data = "";
			$(data.result).each(function(index, item) {
				all_video_data += "<div class=\"common-card_wrapper__Inr_n tw-pb-24\"> " +
					"<div class=\"common-card_wrapper__Inr_n\"> " +
					"<div> " +
					"<div class=\"tw-relative tw-text-common-black\" style=\"font-size: 0px;\"><a " +
					"title=\"" + item.video_introduction +
					"\" href=\"http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video_id + "\" " +
					"target=\"_blank\" class=\"jsx-1095092229\" rel=\"opener\"> " +
					"<div class=\"tw-rounded-4 g-load-img-wrap\" " +
					"style=\"display: block; overflow: hidden; position: relative; z-index: 0;\"> " +
					"<div " +
					"style=\"display: block; box-sizing: border-box; padding-top: 56%;\"> " +
					"</div><img " +
					"src=\"http://120.48.47.144:8899/images/" + item.video_images_url + "\" " +
					"alt=\"" + item.video_introduction + "\" width=\"250\" height=\"140\" class=\"zoom\" " +
					"style=\"position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;\"> " +
					"</div> " +
					"<div " +
					"class=\"tw-w-50 tw-h-20 tw-flex tw-items-center tw-justify-center tw-rounded-2 tw-bg-black tw-bg-opacity-80 tw-text-white tw-text-12 tw-absolute tw-right-8 tw-bottom-8 tw-z-10\"> " +
					"<span class=\"font-size-10_wrapper__pmvsT undefined\" " +
					"style=\"width: 44px; height: 10px;\"><span " +
					"class=\"font-size-10_inner__y3DQ4\"><i " +
					"class=\"DCD_Icon icon_play_1 tw-text-16 tw-mr-2\"></i>11:25</span></span> " +
					"</div> " +
					"</a> " +
					"<div class=\"jsx-1904556275 author-avatar_avatar-wrap__1pWQN\"> " +
					"<div class=\"jsx-2217341492 avatar_avatar-img__170Bh tw-text-0\"><a " +
					"href=\"http://127.0.0.1:8899/html/car_my_homepage.html?" + item.user.user_id + "\" target=\"_blank\" " +
					"rel=\"opener\"> " +
					"<div class=\"avatar tw-rounded-full\" " +
					"style=\"display: inline-block; max-width: 100%; overflow: hidden; position: relative; z-index: 0;\"> " +
					"<div " +
					"style=\"box-sizing: border-box; display: block; max-width: 100%;\"> " +
					"<img src=\"data:image/svg+xml;utf-8,<svg width=&quot;40&quot; height=&quot;40&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>\" " +
					"aria-hidden=\"true\" role=\"presentation\" " +
					"style=\"max-width: 100%; display: block; margin: 0px; border: none; padding: 0px;\"> " +
					"</div><img " +
					"src=\"http://120.48.47.144:8899/images/" + item.user.user_head_portrait_url + "\" " +
					"width=\"40\" height=\"40\" " +
					"style=\"position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;\"> " +
					"</div> " +
					"</a></div> " +
					"<p title=\"" + item.user.user_name + "\" " +
					"class=\"jsx-1904556275 author-avatar_name__3ex3- name line-1\"><a " +
					"href=\"http://127.0.0.1:8899/html/car_my_homepage.html?" + item.user.user_id + "\" title=\"" + item.user.user_name +
					"\" target=\"_blank\" " +
					"rel=\"opener\">" + item.user.user_name + "</a></p> " +
					"</div> " +
					"</div> " +
					"<h3 " +
					"class=\"tw-leading-22 tw-text-16 tw-font-medium tw-text-common-black tw-text-justify line-2 g-active-link-text tw-mt-36\"> " +
					"<a title=\"" + item.video_introduction + "\" " +
					"href = \"http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video_id + "\" " +
					"target=\"_blank\" class=\"jsx-1095092229\" rel=\"opener\">" + item.video_introduction +
					"</a> " +
					"</h3> " +
					"<p class=\"tw-mt-8 tw-h-20 tw-lead tw-text-common-gray tw-text-14\"><a " +
					"title=\"" + item.video_introduction +
					"\" href=\"http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video_id + "\" " +
					"target=\"_blank\" class=\"jsx-1095092229\" rel=\"opener\"><span " +
					"class=\"tw-mr-8\">" + (item.video_broadcast_count > 10000 ? (item
						.video_broadcast_count / 10000).toFixed(2) + "万" : item.video_broadcast_count) +
					"次播放</span><span>" + item.video_upload_date + "</span></a></p> " +
					"</div> " +
					"</div> " +
					"</div>";
			});
			$("#all_video").append(all_video_data);
		}, "json");
}

/* 懒加载刷新页面 */
window.onscroll = function() {
	var scroll_top = document.body.scrollTop || document.documentElement.scrollTop;
	var win_top = window.innerHeight;
	if (scroll_top + win_top > load_leng && page_index != pages) {
		page_index++;
		load_all_video(old_car_series_id);
		load_leng += 500;
	}
}
